package com.clear.subject.application.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Feign请求拦截器
 */
public class FeginRequestInterceptor implements RequestInterceptor {

    /**
     * 该方法专门对fegin请求做处理
     *
     * @param requestTemplate
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        if (!Objects.isNull(request)) {
            String loginId = request.getHeader("loginId");
            if (StringUtils.isNoneBlank(loginId)) {
                // 通过此步骤将loginId传递给 auth微服务
                requestTemplate.header("loginId", loginId);
            }
        }
    }
}
